import { defineEventHandler, getCookie, readBody, createError } from 'h3';
import { BackupManager } from '../../utils/backup.js';

export default defineEventHandler(async (event) => {
  try {
    const userId = getCookie(event, 'userId');
    if (!userId) {
      throw createError({ statusCode: 401, statusMessage: 'Unauthorized' });
    }

    const { filename } = await readBody(event);
    if (!filename) {
      throw createError({ statusCode: 400, statusMessage: 'Filename is required' });
    }

    const backupManager = new BackupManager();
    const result = await backupManager.restoreBackup(filename);

    return {
      success: true,
      message: '备份恢复成功',
      safetyBackup: result.safetyBackup
    };
  } catch (error) {
    console.error('恢复备份失败:', error);
    throw createError({ statusCode: 500, statusMessage: 'Failed to restore backup' });
  }
});
